﻿using System.Collections.Generic;
using System.Text;
using Dapper;
using Eagles.Application.Model.Branch.Requset;
using Eagles.Base.Cache;
using Eagles.Base.DataBase;
using Eagles.DomainService.Model.Org;
using Eagles.DomainService.Model.User;
using Eagles.Interface.DataAccess;

namespace Eagles.DomianService.DataAccess
{
    public class BranchDataAccess : IBranchDataAccess
    {
        private readonly IDbManager dbManager;

        private readonly ICacheHelper cacheHelper;

        public BranchDataAccess(IDbManager dbManager, ICacheHelper cacheHelper)
        {
            this.dbManager = dbManager;
            this.cacheHelper = cacheHelper;
        }

        public List<TbBranch> GetBranchList(GetBranchRequset requset, out int totalcount,int orgId)
        {
            var sql = new StringBuilder();
            var parameter = new StringBuilder();
            var dynamicParams = new DynamicParameters();

            if (orgId > 0)
            {
                parameter.Append(" and OrgId = @OrgId ");
                dynamicParams.Add("OrgId", orgId);
            }


            if (!string.IsNullOrWhiteSpace(requset.BranchName))
            {
                parameter.Append(" and BranchName like @BranchName ");
                dynamicParams.Add("BranchName", '%' + requset.BranchName + '%');
            }



            sql.AppendFormat(@"SELECT count(*)
FROM `eagles`.`tb_branch`  where 1=1  {0} ;
 ", parameter);
            totalcount = dbManager.ExecuteScalar<int>(sql.ToString(), dynamicParams);

            sql.Clear();

            dynamicParams.Add("pageStart", (requset.PageNumber - 1) * requset.PageSize);
            dynamicParams.Add("pageNum", requset.PageNumber);
            dynamicParams.Add("pageSize", requset.PageSize);




            sql.AppendFormat(@" SELECT `tb_branch`.`OrgId`,
    `tb_branch`.`BranchId`,
    `tb_branch`.`BranchName`,
    `tb_branch`.`BranchDesc`,
    `tb_branch`.`CreateTime`,
    `tb_branch`.`Secretary`,
    `tb_branch`.`ViceSecretary`
FROM `eagles`.`tb_branch`  where 1=1    {0}  order by BranchId desc limit  @pageStart ,@pageSize
 ", parameter);




            return dbManager.Query<TbBranch>(sql.ToString(), dynamicParams);
        }

        public TbBranch GetBranchDetail(GetBranchDetailRequset requset)
        {
            var sql = new StringBuilder();
            var dynamicParams = new DynamicParameters();

            sql.Append(@"  SELECT `tb_branch`.`OrgId`,
    `tb_branch`.`BranchId`,
    `tb_branch`.`BranchName`,
    `tb_branch`.`BranchDesc`,
    `tb_branch`.`CreateTime`,
    `tb_branch`.`Secretary`,
    `tb_branch`.`ViceSecretary`
FROM `eagles`.`tb_branch`  where BranchId=@BranchId;
 ");
            dynamicParams.Add("BranchId", requset.BranchId);

            return dbManager.QuerySingle<TbBranch>(sql.ToString(), dynamicParams);
        }

        public List<TbBranch> GetBranchList(List<int> list)
        {

           

            var sql = new StringBuilder();
            var dynamicParams = new DynamicParameters();

            sql.Append(@" SELECT `tb_branch`.`OrgId`,
    `tb_branch`.`BranchId`,
    `tb_branch`.`BranchName`,
    `tb_branch`.`BranchDesc`,
    `tb_branch`.`CreateTime`,
    `tb_branch`.`Secretary`,
    `tb_branch`.`ViceSecretary`
FROM `eagles`.`tb_branch`  where  BranchId  in @BranchId
 ");
            dynamicParams.Add("BranchId", list.ToArray());

            return dbManager.Query<TbBranch>(sql.ToString(), dynamicParams);

           
        }

        public List<TbBranch> GetBranchByOrgId(int orgId)
        {
            var sql = new StringBuilder();
            var dynamicParams = new DynamicParameters();
            sql.Append(@"SELECT a.OrgId,a.BranchId,a.BranchName,a.BranchDesc,a.CreateTime,a.Secretary,a.ViceSecretary,
(select count(*) from eagles.tb_user_info b where BranchId = a.BranchId) as UserCount FROM eagles.tb_branch a where  OrgId  = @OrgId ");
            dynamicParams.Add("OrgId", orgId);
            return dbManager.Query<TbBranch>(sql.ToString(), dynamicParams);
        }

        public int GetBranchCountOrgId(int list)
        {

            var sql = new StringBuilder();
            var dynamicParams = new DynamicParameters();

            sql.Append(@" SELECT  count(*)
FROM `eagles`.`tb_branch`  where  OrgId  = @OrgId
 ");
            dynamicParams.Add("OrgId", list);

            return dbManager.ExecuteScalar<int>(sql.ToString(), dynamicParams);


        }
        
        public int RemoveBranch(RemoveBranchRequset requset)
        {
            return dbManager.Excuted(@"  DELETE FROM `eagles`.`tb_branch`  where BranchId=@BranchId;
", new { requset.BranchId });
        }

        public int EditBranch(TbBranch mod)
        {
            return dbManager.Excuted(@"UPDATE `eagles`.`tb_branch`
SET
`OrgId` =@OrgId,
`BranchName` =@BranchName,
`BranchDesc` =@BranchDesc,
`CreateTime` =@CreateTime,
`Secretary` = @Secretary,
`ViceSecretary` = @ViceSecretary
WHERE 
`BranchId` =@BranchId

", mod);
        }

        public int CreateBranch(TbBranch mod)
        {
            return dbManager.Excuted(@"INSERT INTO `eagles`.`tb_branch`
(`OrgId`,
`BranchId`,
`BranchName`,
`BranchDesc`,
`CreateTime`,
`Secretary`,
`ViceSecretary`)
VALUES
(@OrgId,
@BranchId,
@BranchName,
@BranchDesc,
now(),
@Secretary,
@ViceSecretary);

", mod);
        }

        //public List<TB_USER_RELATIONSHIP> GetBranchList(List<UserInfoDetails> list)
        //{
        //    throw new NotImplementedException();
        //}



    }
}
